package com.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pojo.Test;
import com.service.TestService;
import com.vo.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/test")
@CrossOrigin("*")
public class TestAction {

    @Autowired
    private TestService testService;

    /**
     * 新增试卷
     * @param test 页面传的参数
     * @return
     */
    @RequestMapping("/addTest")
    public ResponseBean addTest(Test test){

        // 调用 TestService 中新增试卷
        boolean n=testService.save(test);
        // true 或 false
        return new ResponseBean(0,0,n);
    }

    /**
     * 分页查询所有可用试卷
     * @param page 第几条
     * @param limit 一页几条
     * @param m 搜索条件
     * @return
     */
    @RequestMapping("/searchTest")
    public ResponseBean searchTest(@RequestParam(defaultValue = "1") int page , @RequestParam(defaultValue = "2") int limit , @RequestParam Map m){
        // 调用 TestService 中分页查询所有可用试卷
        IPage iPage  = testService.searchTest(page , limit , m);
        return new ResponseBean(0,iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 根据id修改试卷
     * @param test 参数
     * @return true 或 false
     */
    @RequestMapping("/updateTest")
    public ResponseBean updateTest(Test test){
        boolean n =  testService.updateById(test);
        return new ResponseBean(0,0,n);
    }

}
